<?php
/**
 * Created by PhpStorm.
 * User: qingyun
 * Date: 17/5/2
 * Time: 下午7:17
 */
namespace Admin\Controller;
use Common\Controller\AdminBaseController;
use Think\Think;
use Think\Upload;

class CollectionController extends AdminBaseController{
    public function index(){
        $collection = D('Collection') -> getList();
        $this->assign('list',$collection);
        $this->display();
    }
    public function add(){
        $cates = M('columns') ->field('id,columns')->where(['pid' => '15']) ->select();
        $pid = M('columns') ->field('id,columns')->where('type = 2 AND pid != 0 ') ->select();
        $this -> assign('pid',$pid);
        $this -> assign('cates',$cates);
        $this->display();
    }
    public function save(){
        if(IS_POST){
            $data =I('post.');
            $result = D('Collection') -> addC($data);
            $this->ajaxReturn($result);
        }
    }
    public function img_upload(){
        if($_FILES['file']['size'] > 0){
            $litPath = imgOper('file','Upload/collection/');
            $data['url'] = '/'. $litPath;
            $this ->ajaxReturn($data,'json');
        }else{
            $this->ajaxReturn('上传失败！','json');
        }
    }
    public function del(){
        if(IS_POST){
            $collection = D('collection') -> del(I('post.id/d',0));
            $this->ajaxReturn($collection);
        }
    }
    public function change(){
        if(IS_POST){
            $collection = D('collection') ->change(I('post.id/d',0));
            $this->ajaxReturn($collection);
        }
    }
    public function edit(){
        if(IS_GET){
            $collection = D('collection') -> getInfo(I('get.id/d',0));
            $picture = explode(',',trim($collection['litpic'],','));
            $cates = M('columns') ->field('id,columns')->where(['pid' => '15']) ->select();
            $pid = M('columns') ->field('id,columns')->where('type = 2 AND pid != 0 ') ->select();
            $this -> assign('pid',$pid);
            $this -> assign('cates',$cates);
            if(!empty($picture)){
                $this->assign('picture',$picture);
            }
            $this -> assign('info',$collection);
            $this -> display();
        }
    }
    public function imgdel(){
        if(IS_POST){
            $pic = I('post.pic/d');
            $id = I('post.id/d',0);
            if(empty($id)){
                $this -> ajaxReturn(['操作失败！']);
            }
            $collection = D('collection') -> getInfo($id);
            $picture = explode(',',$collection['litpic']);
            unset($picture[$pic]);
            $litpic = implode(',',$picture);
            $result = M('collection') ->where(['id' => $id]) -> save(['litpic' => $litpic]);
            if($result){
                $this->ajaxReturn(['操作成功！']);
            }else{
                $this -> ajaxReturn(['操作失败！']);
            }
        }
    }
    public function info(){
        if(IS_GET){
            $id = I('get.id/d',0);
            $info = D('collection') -> getInfo($id);
            $picture = explode(',',trim($info['litpic'],','));
            if(!empty($picture)){
                $this->assign('picture',$picture);
            }
            $this -> assign('info',$info);
            $this ->display();
        }
    }
    public function editSave(){
        if(IS_POST){
            $data =I('post.');
            $result = D('Collection') -> saveC($data);
            $this->ajaxReturn($result);
        }
    }
}